// pages/under-door/in-survey/index.js

import api from '../../../api/index';
import {
  uploadFileTest
} from '../../../utils/util';


// 调查中
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    housePhotos: [], //房屋照片
    video: null, //视频
    signPictures: [], //签约图片
    sfzReverse: null, //身份证反面
    sfzPositive: null, //身份证正面
    housePropertyCard: [], //房产证图片
    housingAppearance: [], //房屋外观照
    assessmentFile: [], //评估文件
    tbSurveyAccessoryList: [], //提交的所有附件---传给后端
    formData: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id,
      formData: options.data
    })
    if (this.data.id) {
      this.getSurveyAccessoryBySid();
    }
  },
  //获取图片信息
  getSurveyAccessoryBySid() {
    api.getSurveyAccessoryBySid({
      sid: this.data.id
    }).then(res => {
      this.setData({
        formData: res.data.data || {}
      })

    })
  },

  //删除图片
  delImg(e) {
    let {
      type,
      index
    } = e.currentTarget.dataset;
    let data = this.data.formData['type' + type];
    api.deleteImg(data[index].id).then(res => {
      data.splice(index, 1);
      this.setData({
        [`formData.${['type'+type]}`]: data
      })
    })

  },

  //选择图片
  chooseImg(e) {
    let _this = this;
    let {
      type
    } = e.currentTarget.dataset;
    wx.chooseImage({
      count:type==0?1:9,
      sizeType: ['original', 'compressed'],
      sourceType: ['camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFiles = res.tempFiles;
        if (type == 0) {
          _this.upload(tempFiles[0], type)
        } else {
          _this.upload(tempFiles, type)
        }
       
      }
    })
  },

  //选择视频
  chooseVideo() {
    let _this = this;
    wx.chooseVideo({
      sourceType: ['camera'],
      maxDuration: 60,
      camera: 'back',
      success(res) {
        _this.upload(res, 2);
      }
    })
  },

  //保存数据
  addArriveImg2(obj) {
    return api.addArriveImg2(obj).then(res => {
      return res.data.data;
    })
  },

  //获取上传
  async upload(arr, num) {
    //num 0-6  分别为 身份证  房产证  房屋视频  签约文件 房屋外观  房屋照    评估文件
    //单图/视频上传
    if (num == 2 || num == 0) {
      let ossRes = await api.surveyGetMap();
      let credentials = ossRes.data.data;
      let itemArr = [];
      let suffix = '';
      let url = '';
      //传身份证
      if (num == 0) {
        itemArr = arr.path.split('.');
        suffix = itemArr[itemArr.length - 1];
        url = await uploadFileTest(arr.path, credentials, suffix);
        console.log(url)
      } else if (num == 2) {
        //传视频
        itemArr = arr.tempFilePath.split('.');
        suffix = itemArr[itemArr.length - 1];
        url = await uploadFileTest(arr.tempFilePath, credentials, suffix);
      }
      let obj = {
        url,
        type: num,
        size: arr.size,
        sid: this.data.id,
        showName: url,
      }
      let resData = await this.addArriveImg2(obj);
      let data = this.data.formData;
      if(!Array.isArray(data['type'+num])){
        data['type'+num]=[];
      }
      data['type' + num].push(resData);
      this.setData({
        formData: data
      })
    } else {
      //多图上传
      for (let index = 0; index < arr.length; index++) {
        let ossRes = await api.surveyGetMap();
        let credentials = ossRes.data.data;
        let itemArr = arr[index].path.split('.');
        let suffix = itemArr[itemArr.length - 1];
        let url = await uploadFileTest(arr[index].path, credentials, suffix);
        let obj = {
          url,
          type: num,
          size: arr[index].size,
          sid: this.data.id,
          showName: url,
        }
        let resData = await this.addArriveImg2(obj);
        console.log(resData)

        let data = this.data.formData;

        if (!Array.isArray(data['type' + num])) {
          data['type' + num] = [];
        }
        data['type' + num].push(resData);
        this.setData({
          formData: data
        })

      }

    }
    return true;

  },
  //提交
  async formSubmit() {
    console.log('提交.............')
    if (this.data.sfzReverse) {
      await this.upload(this.data.sfzReverse, 0); //身份证反面
    }
    if (this.data.sfzPositive) {
      await this.upload(this.data.sfzPositive, 0); //身份证正面
    }
    if (this.data.housePropertyCard.length) {
      await this.upload(this.data.housePropertyCard, 1); //房产证图片
    }
    if (this.data.housingAppearance.length) {
      await this.upload(this.data.housingAppearance, 4); //房屋外观照
    }
    if (this.data.housePhotos.length) {
      await this.upload(this.data.housePhotos, 5); //房屋照片
    }
    if (this.data.signPictures.length) {
      await this.upload(this.data.signPictures, 3); //签约文件图片
    }
    if (this.data.assessmentFile.length) {
      await this.upload(this.data.assessmentFile, 6); //评估文件
    }
    if (this.data.video) {
      await this.upload(this.data.video, 2); //视频
    }
    console.log('111', this.data.formData);
    console.log('222', this.data.tbSurveyAccessoryList);
    let data = {
      tbSurveyAccessoryList: this.data.tbSurveyAccessoryList,
      tbSurveyData: JSON.parse(this.data.formData)
    }
    api.surveyAddData(data).then(res => {
      wx.showToast({
        title: '提交成功',
      })
      wx.navigateBack({
        delta: 3,
      })
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})